//
//  DeviceTool.swift
//  WalkerLove
//
//  Created by Z on 2020/4/18.
//  Copyright © 2020 Carlson. All rights reserved.
//

import UIKit
import AdSupport
import CommonCrypto

class DeviceTool: NSObject {

    //处理头文件
    static func configureRequestHeader() -> [String:String] {
        return configureRequestHeaderWithType(type: 2)
    }
    
    static func configureRequestHeaderWithType(type: NSInteger) -> [String:String] {
        let request_agent = "2"
        let device_id = deviceId()
        let os_version = "1"
        let sdk_version = systemVersion()
        let phone_model = machineModelName()
        let market = "appStore"
        let app_version = appVersion()
        let app_name = "1"
        let request_id = ret32bitString()
        let app_id = "ae3383fa060446e690cf79571d35144a"
        let app_secret = "87860bd6d0af46cc962ebd2bd626a65b"
        let timestamp = String(format: "%.f", NSDate().timeIntervalSince1970*1000)
        let sign = sha256AppId(appid: app_id, secret: app_secret, timestamp: timestamp)
        let customer_id = ""//登录后必传
        let access_token = ""//访问令牌
        let sm_deviceid = ""
        let longitude = ""
        let latitude = ""
        let gtid = ""
        return ["request-agent":request_agent, "device-id":device_id, "sdk-uid":"", "os-version":os_version, "sdk-version":sdk_version, "phone-model":phone_model, "market":market, "app-version":app_version, "app-name":app_name, "request-id":request_id, "app-id":app_id, "timestamp":timestamp, "sign":sign, "customer-id":customer_id, "access-token":access_token, "sm-deviceid":sm_deviceid, "gps-lng":longitude, "gps-lat":latitude, "gt-id":gtid]
    }
    
    //设备id
    static func deviceId() -> String {
        var distinctId = ASIdentifierManager.shared().advertisingIdentifier.uuidString
        if distinctId.isEmpty {
            distinctId = UIDevice.current.identifierForVendor?.uuidString as! String
        }
        if(distinctId.isEmpty){
            distinctId = NSUUID.init().uuidString
        }
        return distinctId
    }
    //系统版本
    static func systemVersion() -> String {
        return UIDevice.current.systemVersion
    }
    //手机型号
    static func machineModelName() -> String {
        return "iphone"
    }
    //app版本
    static func appVersion() -> String {
        let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"]
        if let v = version as? String {
            return v.replacingOccurrences(of: " ", with: "")
        }
        return ""
    }
    //随机生成
    static func ret32bitString() -> String {
        var data = String()
        let strAll = "0123456789abcdefghijklmnopqrstuvwxyz"
        
        for _ in 0..<32 {
            let rIdx = Int(arc4random()%32)
            let rIndex = strAll.index(strAll.startIndex, offsetBy: (rIdx as! String.IndexDistance))
            data.append(strAll[rIndex])
        }
        return data
    }
    //签名
    static func sha256AppId(appid: String, secret: String, timestamp: String) -> String {
        let a = appid.appending(timestamp)
        return hmacSHA256String(hStr: a, secret: secret)
    }
    static func hmacSHA256String(hStr: String, secret: String) -> String {
        let data = hStr.cString(using: .utf8)
        let size = CC_SHA256_DIGEST_LENGTH
        var result = [CUnsignedChar](repeating: 0, count: Int(size))
        let cKey = secret.cString(using: .utf8)
        CCHmac(CCHmacAlgorithm(kCCHmacAlgSHA256), cKey, strlen(cKey!), data, strlen(data!), &result)
        let hash = String.init(cString: result)
        return hash
    }
    
    static func ColorFromRGB(_ _rgbValue_: UInt32) -> UIColor {
        return UIColor(red: (CGFloat((_rgbValue_ & 0xFF0000) >> 16))/255.0, green: (CGFloat((_rgbValue_ & 0xFF00) >> 8))/255.0, blue: (CGFloat(_rgbValue_ & 0xFF))/255.0, alpha: 1.0)
    }
}
